<template>
  <div>
    <Myheader>
      <template #center>dululu (#^.^#) kiana register hualala</template>
      <template #os>注册注册~~~</template>
    </Myheader>
    <WhiteSpace :space="50" />
    <div class="container">
      <div class="left">
        <div class="bgi"></div>
      </div>
      <div class="right">
        <div class="form">
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="账号" prop="user">
              <el-input type="text" v-model="ruleForm.user" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="nick">
              <el-input type="text" v-model="ruleForm.nick" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="bottom">
          已有账号？<span @click="$router.push('/shop/login')">立即登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { doRegister } from '@/service/shop';

export default {
  data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validateCheckPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else {
          if (this.ruleForm.checkPass !== this.ruleForm.pass) {
            callback(new Error('两次密码不一致，请重新输入'));
          }
          callback();
        }
      };
      var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        }  else {
          callback();
        }
      };
      var validateNick = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入昵称'));
        }  else {
          callback();
        }
      };
      return {
        ruleForm: {
          user: '',
          pass: '',
          checkPass: '',
          nick: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validateCheckPass, trigger: 'blur' }
          ],
          user: [
            { validator: validateUser, trigger: 'blur' }
          ],
          nick: [
            { validator: validateNick, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            const res = await doRegister(this.ruleForm);
            console.log(res);
            const { code, message } = res || {};
            if (code === 0) {
              return this.$message.warning({
              message: message,
              duration: 1500,
              showClose: true,
            });
            }
            if (code === 1) {
              this.$message.success({
              message: message,
              duration: 1500,
              showClose: true,
            });
            return this.$router.push('/shop/login');
            } 
          } else {
            return this.$message.error({
              message: '请完整填写表单',
              duration: 1500,
              showClose: true,
            })
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
};
</script>

<style lang="scss" scoped>
.container {
  width: 70vw;
  height: 42vh;
  margin: 0 auto;
  display: flex;
  border: 1px solid pink;
  box-sizing: border-box;
  border-radius: 20px;
  box-shadow: 1px 2px 2px #ccc;
  overflow: hidden;
  .left {
    width: 20vw;
    // border-radius: 20px;
    background: url('@/assets/shenli.png');
    background-size: contain;

  }
  .right {
    width: 40vw;
    padding: 30px 20px 10px 0;
    box-sizing: border-box;
    .form {
      height: 35vh;
    }
    .bottom {
      height: 3vh;
      text-align: center;
      font-size: 14px;

      span {
        font-size: 20px;
        cursor: pointer;
        &:hover {
          color: skyblue;
        }
      }
    }
  }
}
</style>
